package jsd2204.bookstore.bookserver.service.impl;

import jsd2204.bookstore.bookserver.ex.ServiceException;
import jsd2204.bookstore.bookserver.mapper.CommentMapper;
import jsd2204.bookstore.bookserver.pojo.dto.CommentDTO;
import jsd2204.bookstore.bookserver.pojo.entity.Comment;
import jsd2204.bookstore.bookserver.pojo.vo.ComsStandardVO;
import jsd2204.bookstore.bookserver.service.CommentService;
import jsd2204.bookstore.bookserver.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author jiale
 * @version 1.0
 * @description这是评论接口的实现类
 * @date 2022/08/02 12:30
 **/
@Slf4j
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    CommentMapper mapper;
    public CommentServiceImpl(){
        log.debug("创捷业务对象:CommentServiceImpl");
    }

    @Override
    public void comment(CommentDTO commentDTO) {
        Comment comment=new Comment();
        comment.setContent(commentDTO.getContent());
        comment.setBookId(commentDTO.getBookId());
        comment.setUserId(2L);
        comment.setUserNick("小红");
        comment.setPhoto("小红.jpg");
        int row = mapper.commentInsert(comment);
        if (row!=1) {
            String message="评论失败，服务器繁忙，请稍后再试";
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }

    @Override
    public List<ComsStandardVO> comsByID(Long id) {
        List<ComsStandardVO> comsStandardVOList = mapper.comsById(id);
        return comsStandardVOList;
    }

    @Override
    public void commentDeleteById(Long id) {
        int row = mapper.commentDeleteById(id);
        if(row!=1){
            String message="删除失败，服务器繁忙，请稍后再试";
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
    }
}
